﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Common.DTO;
using DataLayer;

namespace LogicLayer
{
    /// <summary>
    /// TODO: Comment
    /// </summary>
   public class GameHandler
   {
       /// <summary>
       /// Creates a not randomized gamesession. A gamesession(Gamesession) is created and a 
       /// playergamesession(SpelaresSpelsessioner) is added.
       /// </summary>
       /// <param name="playerId"></param>
       /// <param name="gameId"></param>
       /// <param name="rounds">number of rounds the player wants to play</param>
       /// <returns>boolean</returns>
       public static bool CreateGamesession(int playerId, int gameId, int rounds)
       {
           var player = GetDBInfo.GetPlayerById(playerId);
           var game = GetDBInfo.GetGameById(gameId);

           int gameSessionId = SaveDBInfo.CreateGameSession(game.ID, rounds);

           int gamePlayerSessionId = SaveDBInfo.CreatePlayerSession(gameSessionId, player.ID);
           if ((gameSessionId != null && gamePlayerSessionId != null) || (gamePlayerSessionId > 0 && gameSessionId > 0))
               return true;
           else
               return false;
       }

       /// <summary>
       /// Deletes a PlayerSession
       /// </summary>
       /// <param name="playerId"></param>
       /// <param name="sessionId"></param>
       /// <returns></returns>
       public static bool DeletePlayerSession(int playerId, int sessionId)
       {
           if (SaveDBInfo.DeletePlayerSession(playerId, sessionId))
               return true;
           else
               return false;
       }

       public static bool DeleteGameSession(int gameSessionId)
       {
           if (SaveDBInfo.DeleteGameSession(gameSessionId))
               return true;
           else
               return false;
       }

       public static List<GameSession> GetGameSession(string namn)
       {
           var spelSession = GetDBInfo.GetGameSessionListInfoDB(namn);

           var spelLista = new List<GameSession>();
           //foreach (var s in (IEnumerable) spel)
           //{
             //  spelLista.Add(new GameSession{Rounds = s.antalomgångar, GameObject = {new GameObject{GameId = s.spelid, GameName = "vet i fan"}}, GameSessionId = s.id;
               
           //}

            //spelLista.Add(new GameSession{Rounds = 3, GameSessionId = 22, GameObject = new GameObject{GameId = 1, GameName = spel.ToString()}});

           return spelLista;
       }

       public static List<Game> GetGameList()
       {
           var games = GetDBInfo.GetGameListInfoDb();

           return  games.Select(game => new Game {ID = game.ID, Name = game.Name}).ToList();
       }

       public static List<GameSession> GetAllActiveGameSessions()
       {
           var session = GetDBInfo.GetActiveGameSessionInfoDb();

           if (session == null)
           {
               return null;
           }

           return
               session.Select(
                   s =>
                   new GameSession
                       {
                           ID = s.Id,
                           GameID = s.GameId,
                           NameOfGame = GetDBInfo.GetGameById(s.GameId).Name,
                           NumOfGameRoundsToPlay = s.NumbersOfRounds
                       }).ToList();
       }

       public static bool UpdateGameRounds(int id, int rounds)
       {
           if (SaveDBInfo.UpdateGameRounds(id, rounds))
           {
               return true;
           }
           else
               return false;
           
       }

       public static List<GameSession> GetAllJoinableGameSessions(int gameID, int playerId)
       {
           var sessions = GetDBInfo.GetJoinableGameSessions(gameID, playerId);

           return
               sessions.Select(
                   s =>
                   new GameSession
                       {
                           ID = s.Id,
                           GameID = s.GameId,
                           NameOfGame = GetDBInfo.GetGameById(s.GameId).Name,
                           NumOfGameRoundsToPlay = s.NumbersOfRounds
                       }).ToList();
       }

       public static List<GameSession> GetPlayersGameSessions(int playerID)
       {
           var sessions = GetDBInfo.GetPlayersGameSessions(playerID);

           return
               sessions.Select(
                   s =>
                   new GameSession
                   {
                       ActiveGame = true,
                       ID = s.ID,
                       GameID = s.GameID,
                       NameOfGame = GetDBInfo.GetGameById(s.GameID).Name,
                       NumOfGameRoundsToPlay = s.NoOfGameRounds ?? 3
                   }).ToList();
       }

       public static Player GetSessionWinner(int gameSessionId)
       {
           return GetDBInfo.DetermineSessionWinner(gameSessionId);
       }

   }
}
 